package cn.com.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.com.entity.Departments;

public class DepartmentsDao {
	//科室的dao文件
	private  SessionFactory sessionFac;
	
	public SessionFactory getSessionFac() {
		return sessionFac;
	}
	public void setSessionFac(SessionFactory sessionFac) {
		this.sessionFac = sessionFac;
	}
	Session session;
	Transaction tr;
	//全部科室的查询
	public List<Departments> selectAlldeDepartments(){
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		sessionFac=(SessionFactory)ac.getBean("sessionFac");
		session=sessionFac.openSession();
		Query query=session.createQuery("from Departments");
		List<Departments> list=query.list();
		session.close();
		return list;
	}
	//查询一个科室
	public Departments selectOneDepartments(Departments departments){
		session=sessionFac.openSession();
		Departments depart=null;
		Query query=session.createQuery("from Departments where departmentsid=?");
		query.setInteger(0, departments.getDepartmentsid());
		List<Departments> list=query.list();
		if(list!=null){
			depart=list.get(0);
		}
		return depart;
	}
	
	//通过科室id号码，查询一个科室
	public Departments selectOneDepartments(int departmentsid){
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		sessionFac=(SessionFactory)ac.getBean("sessionFac");
		session=sessionFac.openSession();
		Departments depart=null;
		Query query=session.createQuery("from Departments where departmentsid=?");
		query.setInteger(0, departmentsid);
		List<Departments> list=query.list();
		if(list!=null){
			depart=list.get(0);
		}
		return depart;
	}
	//通过传入的值，判断
	public List<Departments> selectDepartments(Departments departments){
			session=sessionFac.openSession();
			if(departments.getDepartmentsid()==0){
				Query query=session.createQuery("from Departments");
				List<Departments> list=query.list();
				session.close();
				return list;
			}
			else{
				Query query=session.createQuery("from Departments where departmentsid=?");
				query.setInteger(0, departments.getDepartmentsid());
				List<Departments> list=query.list();
				session.close();
				return list;
			}
		
	}
	
	
	//增加科室
	public boolean addDepartments(Departments departments){
		boolean bool=false;
		session=sessionFac.openSession();
		tr=session.beginTransaction();
		try {
			session.save(departments);
			bool=true;
			tr.commit();
			session.close();
			return bool;
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return bool;
		}
	}
	//更新科室
	public boolean updateDepartments(Departments departments){
		boolean bool=false;
		session=sessionFac.openSession();
		tr=session.beginTransaction();
		try {
			session.update(departments);
			bool=true;
			tr.commit();
			session.close();
			return bool;
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return bool;
		}
	}
	//删除科室
	public boolean deleteDepartments(Departments departments){
		boolean bool=false;
		session=sessionFac.openSession();
		tr=session.beginTransaction();
		try {
			session.delete(departments);
			bool=true;
			tr.commit();
			session.close();
			return bool;
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return bool;
		}
	}
}
